var DevelopperToolbar = new Class({
	initialize: function(){
		this.toolbar = $('SLS_DevelopperToolbar');
		this.toolbarOver = false;
		
		this.setToolbar();
		this.activateToolbar();
		this.menuNavigation();
		this.syntaxHighlighter();
		this.toolbar.inject($$('body')[0]);
	},
	
	setToolbar: function(){
		var positions = { closed : -(this.toolbar.getCoordinates().height+3), opened : 0};
		this.toolbar.store('positions', positions);
		this.toolbar.store('fx', new Fx.Morph(this.toolbar, {duration:150, link:'cancel'}));
		this.toolbar.setStyles({'top' : this.toolbar.retrieve('positions').closed});
		
		this.toolbar.getElements('ul.sls_dt_main_menu > li > ul').addClass('sls_dt_sub_menu');
		this.toolbar.getElements('ul.sls_dt_main_menu > li > ul > li > ul').addClass('sls_dt_side_menu');
		this.toolbar.getElements('ul.sls_dt_main_menu li a').set('target', 'blank');
		this.toolbar.getElements('ul.sls_dt_sub_menu').each(function(subMenu){
			var width = 0;
			var items = subMenu.getChildren('li');
			items.each(function(item){
				var widthItem = item.measure(function(){
					return this.getCoordinates().width-parseInt(this.getStyle('padding-right'));
				});
				
				if (widthItem > width) width = widthItem;
			});
			
			items.setStyles({'width' : width});
		});
	},
	
	activateToolbar: function(){
		this.openButton = this.toolbar.getElement('span.sls_dt_open');
		this.openButton.addEvents({
			'click': function(){
				this.toggleToolbar();
			}.bind(this)
		});
	},
	
	toggleToolbar: function(){
		if (this.toolbar.hasClass('sls_dt_state_opened')){
			this.closeToolbar();
		} else {
			this.openToolbar();
		}
	},
	
	closeToolbar: function(){
		this.toolbar.retrieve('fx').start({'top' : this.toolbar.retrieve('positions').closed})
		this.toolbar.removeClass('sls_dt_state_opened').addClass('sls_dt_state_closed');
	},
	
	openToolbar: function(){
		this.toolbar.retrieve('fx').start({'top' : this.toolbar.retrieve('positions').opened})
		this.toolbar.removeClass('sls_dt_state_closed').addClass('sls_dt_state_opened');
	},
	
	menuNavigation: function(){
		this.toolbar.getElements('ul.sls_dt_main_menu li').each(function(li){
				if (li.getElements('ul').length > 0){
					var ul = li.getElement('ul');
				
					li.addEvents({
						'mouseenter': function(){
							
							if (li.getElements('ul').length > 0)
								ul.setStyles({'display' : 'block'});
						}.bind(this),
						
						'mouseleave': function(){
							
							if (li.getElements('ul').length > 0)
								ul.setStyles({'display' : 'none'});
						}.bind(this)
					});
				}
		}.bind(this));
		
		this.toolbar.getElements('ul.sls_dt_main_menu ul.sls_dt_sub_menu > li').each(function(li){
			li.set('morph', {link:'cancel', duration:100});
			li.store('width', li.measure(function(){return (this.getCoordinates().width-parseInt(this.getStyle('padding-right')));}));
			li.addEvents({
				'mouseenter': function(){
					li.morph({'margin-left' : 5, 'width' : (li.retrieve("width")-5), 'background-color' : '#A57FD5'});
				},
				'mouseleave': function(){
					li.morph({'margin-left' : 0, 'width' : li.retrieve("width"), 'background-color' : '#837FD5'});
				}
			});
		});
		
		this.toolbar.getElements('ul.sls_dt_main_menu ul').each(function(ul){
			var top= 0;
			if (!ul.hasClass('sls_dt_side_menu'))
				top = 35;
				
			ul.addEvent('mousewheel', function(e){
				e.stop();
				if (e.wheel > 0 && ul.getCoordinates(ul.getParent()).top < top)
					ul.setStyles({'top' : ul.getCoordinates(ul.getParent()).top+20});
				else if (e.wheel < 0 && (ul.getCoordinates().top+ul.getCoordinates().height) >= window.getHeight())
					ul.setStyles({'top' : ul.getCoordinates(ul.getParent()).top-20});
			});
		});
	},
	
	syntaxHighlighter: function(){
		
		this.toolbar.getElements('li').each(function(li, index){
			if (li.innerHTML.match(/^array.*/)){
				li.innerHTML = "<pre name='highlight_"+index+"' class='php:nocontrols'>"+html_entity_decode(li.innerHTML)+"</pre>";
				dp.SyntaxHighlighter.HighlightAll('highlight_'+index);
				li.addClass('syntax_highlighter');
			}
		});
	}
});

window.addEvent('domready', function(){
	new DevelopperToolbar();
});